package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ISmsFlashPromotionService;
import com.itheima.sms.entity.SmsFlashPromotion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {

    @Autowired
    private ISmsFlashPromotionService smsFlashPromotionService;

    /**
     * 添加活动
     * @param
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody SmsFlashPromotion smsFlashPromotion){
       smsFlashPromotionService.save(smsFlashPromotion);
        return AjaxResult.success();
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Long id){
        smsFlashPromotionService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 根据活动名称分页查询
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "keyword", required = false) String keyword,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize){

        Page<SmsFlashPromotion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, SmsFlashPromotion::getTitle, keyword);
        smsFlashPromotionService.page(page, lqw);
        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);
    }

    /**
     * 修改上下线状态
     * @param id
     * @param map
     * @return
     */
    @PostMapping("/update/status/{id}")
    public AjaxResult updateStatus(@PathVariable Long id,@RequestParam("status") String status){
        LambdaUpdateWrapper<SmsFlashPromotion> luw = new LambdaUpdateWrapper<>();
        luw.eq(SmsFlashPromotion::getId, id)
                .set( SmsFlashPromotion::getStatus,status);

        smsFlashPromotionService.update(luw);
        return AjaxResult.success();

    }

    /**
     * 编辑活动
     * @param id
     * @param smsFlashPromotion
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateById(@PathVariable Long id,@RequestBody SmsFlashPromotion smsFlashPromotion){
        smsFlashPromotionService.updateById(smsFlashPromotion);
        return AjaxResult.success();
    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id){
        SmsFlashPromotion byId = smsFlashPromotionService.getById(id);
        return AjaxResult.success(byId);
    }

}
